<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="88px">
            <el-form-item label="姓名" prop="username">
                <el-input v-model="queryParams.username" placeholder="请输入姓名" clearable size="small" maxlength="11"
                    @input="handleQuery" />
            </el-form-item>
            <el-form-item label="微信手机号" prop="telephone">
                <el-input v-model="queryParams.telephone" placeholder="请输入微信手机号" clearable size="small" maxlength="11"
                    @input="handleQuery" />
            </el-form-item>
            <el-form-item label="类型" prop="type">
                <el-select v-model="queryParams.type" placeholder="请选择类型" clearable size="small" @change="handleQuery">
                    <!-- <el-option label="充值" value="1" /> -->
                    <el-option label="消费（订餐）" value="2" />
                  <el-option label="消费（购物）" value="3" />
                </el-select>
            </el-form-item>
            <el-form-item label="就餐类型" prop="diningType">
                <el-select v-model="queryParams.diningType" placeholder="请选择就餐类型" clearable size="small"
                    @change="handleQuery">
                    <el-option label="早餐" value="1" />
                    <el-option label="午餐" value="2" />
                    <!-- <el-option label="晚餐" value="3" />
                    <el-option label="接待餐" value="4" /> -->
<!--                    <el-option label="其它" value="5" />-->
                </el-select>
            </el-form-item>

            <el-form-item label="消费时间" prop="timeArr">
                <el-date-picker @change="handleQuery" v-model="queryParams.timeArr" type="daterange"
                    format="yyyy年MM月dd日" value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始日期"
                    end-placeholder="结束日期">
                </el-date-picker>
            </el-form-item>



          <el-form-item
            label="部门"
            prop="department"
          >
            <template>
              <el-select
                v-model="queryParams.department"
                collapse-tags
                style="margin-left: 20px;width:300px"
                placeholder="请选择部门"
              >
                <el-option
                  v-for="item in options"
                  :key="item.deptId"
                  :label="item.deptName"
                  :value="item.deptId"
                >
                </el-option>
              </el-select>
            </template>
          </el-form-item>

            <!--      <el-form-item label="操作人" prop="operator">-->
            <!--        <el-input-->
            <!--          v-model="queryParams.operator"-->
            <!--          placeholder="请输入操作人"-->
            <!--          clearable-->
            <!--          size="small"-->
            <!--          @keyup.enter.native="handleQuery"-->
            <!--        /> -->
            <!-- </el-form-item> -->
<!--            <el-form-item label="所属公司" prop="companyId">-->
<!--                <el-select v-model="queryParams.companyId" placeholder="请选择所属公司" clearable size="small"-->
<!--                    @change="handleQuery">-->
<!--                    <el-option v-for="item in companys" :key="item.id" :label="item.text" :value="item.id"></el-option>-->
<!--                </el-select>-->
<!--            </el-form-item>-->

<!--          <el-form-item label="导出部门消费统计时间" >-->
<!--            <el-date-picker  v-model="queryParams1.timeArr" type="daterange"-->
<!--                            format="yyyy年MM月dd日" value-format="yyyy-MM-dd" range-separator="至" start-placeholder="开始日期"-->
<!--                            end-placeholder="结束日期">-->
<!--            </el-date-picker>-->
<!--          </el-form-item>-->


            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>

            </el-form-item>
        </el-form>
      <div  STYLE="text-align: right;
               margin-top: -1px;
               margin-right: 110px;">
        <!--                <div> 充值总金额:{{ sumAmount }} 元</div>-->
        <div> 消费总金额:{{ useAmount }} 元</div>
      </div>
        <el-row :gutter="10" class="mb8">
            <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:details:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:details:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:details:remove']"
        >删除</el-button>
      </el-col> -->
            <el-col :span="1.5">
                <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['system:details:export']">导出</el-button>
            </el-col>

          <el-col :span="1.5">
            <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExportForBusiness"
                       v-hasPermi="['system:details:export']">导出部门消费统计</el-button>
          </el-col>

        </el-row>

        <el-table v-loading="loading" :element-loading-text="loadingText" :data="detailsList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column fixed label="序号" align="center">
                <template slot-scope="scope">
                    <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + (scope.$index + 1)}} </span>
                </template>
            </el-table-column>
            <el-table-column label="姓名" align="center" prop="username" />
            <el-table-column label="微信手机号" align="center" prop="telephone" />
            <el-table-column label="类型" align="center" prop="type">
                <template slot-scope="scope">
                    <!-- <span v-if="scope.row.type == 1">充值</span> -->
                    <span v-if="scope.row.type == 2">消费（订餐）</span>
                  <span v-if="scope.row.type == 3">消费（购物）</span>
                  <span v-if="scope.row.type == 4">退款（购物）</span>
                </template>
            </el-table-column>
            <el-table-column label="就餐类型" align="center" prop="diningName" />
            <el-table-column label="消费金额 (元)" align="center" prop="amountStr" />
            <el-table-column label="余额 (元)" align="center" prop="balance" />
<!--            <el-table-column label="所属公司" align="center" prop="companyName" />-->
            <el-table-column label="消费时间" align="center" prop="updateTime" />
          <el-table-column label="部门" align="center" prop="departmentName" />
            <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:details:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:details:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
            @pagination="getList" />

        <!-- 添加或修改【请填写功能名称】对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="用户id" prop="userId">
                    <el-input v-model="form.userId" placeholder="请输入用户id" />
                </el-form-item>
                <el-form-item label="类型  1  充值   2  订餐">
                    <el-select v-model="form.type" placeholder="请选择类型  1  充值   2  订餐">
                        <el-option label="请选择字典生成" value="" />
                    </el-select>
                </el-form-item>
                <!-- <el-form-item label="消费金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入消费金额" />
        </el-form-item>
        <el-form-item label="操作人" prop="operator">
          <el-input v-model="form.operator" placeholder="请输入操作人" />
        </el-form-item>  -->
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listDetails, getDetails, delDetails, addDetails, updateDetails, exportDetails,listDetailsPC ,exportDetailsForBusiness} from "@/api/details/details";
import { getCompanys } from '@/api/system/dept';
import { listDept } from "@/api/system/dept";

export default {
    name: "Details",
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            loadingText:"",
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 总条数
            total: 0,
            // 【请填写功能名称】表格数据
            detailsList: [],
          options:[],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            sumAmount:0,
            useAmount:0,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                userId: undefined,
                type: undefined,
                timeArr: undefined,
                operator: undefined,
                telephone: undefined,
                diningType: undefined,
                startTime: undefined,
                endTime: undefined,
              department:undefined,
              username:undefined,
            },
          queryParams1: {
            timeArr: undefined,
          },
            queryNewParams: false,
            // 表单参数
            form: {},
            // 公司信息
            companys: [],
            // 表单校验
            rules: {
            }
        };
    },
    created() {
        this.getList();
        this.getCompanyList();
        this.getSecondDeptlist();
    },
    methods: {
      /** 获取二级部门列表 */
      getSecondDeptlist() {
        this.loading = true;
        listDept().then((response) => {
          console.log(`getSecondDeptlist:`, response.data);
          this.options = response.data;
        });
      },
        /** 查询【请填写功能名称】列表 */
        getList() {
            this.loading = true;
            this.queryNewParams = JSON.parse(JSON.stringify(this.queryParams))
            if (this.queryNewParams.timeArr && this.queryNewParams.timeArr.length > 0) {
                this.queryNewParams.startTime = this.queryNewParams.timeArr[0]
                this.queryNewParams.endTime = this.queryNewParams.timeArr[1]
                this.queryNewParams.timeArr = ""
                console.log(this.queryParams)
            }
          listDetailsPC(this.queryNewParams).then(response => {
                this.detailsList = response.data.list.list;
                this.sumAmount=response.data.rechargeSum;
                this.useAmount=response.data.useSum;
                this.total = response.data.list.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: undefined,
                userId: undefined,
                type: undefined,
                timeArr: undefined,
                createTime: undefined,
                operator: undefined,
                telephone: undefined,
              department:undefined,
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
          this.queryParams1.timeArr=undefined
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加【请填写功能名称】";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getDetails(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改【请填写功能名称】";
            });
        },
        /** 提交按钮 */
        submitForm: function () {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != undefined) {
                        updateDetails(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess("修改成功");
                                this.open = false;
                                this.getList();
                            }
                        });
                    } else {
                        addDetails(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess("新增成功");
                                this.open = false;
                                this.getList();
                            }
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$confirm('是否确认删除【请填写功能名称】编号为"' + ids + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function () {
                return delDetails(ids);
            }).then(() => {
                this.getList();
                this.msgSuccess("删除成功");
            }).catch(function () { });
        },
        /** 导出按钮操作 */
        handleExport() {
            let arr = {
                startTime: this.queryParams.timeArr ? this.queryParams.timeArr[0] : '',
                endTime: this.queryParams.timeArr ? this.queryParams.timeArr[1] : '',
                diningType: this.queryParams.diningType,
                operator: this.queryParams.operator,
                pageNum: this.queryParams.pageNum,
                pageSize: this.queryParams.pageSize,
                telephone: this.queryParams.telephone,
                type: this.queryParams.type,
                userId: this.queryParams.userId,
                department: this.queryParams.department,
              username: this.queryNewParams.username,
            }
            this.$confirm('是否确认导出所有数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(()=> {
              this.loadingText = "正在导出中..."
              this.loading = true;
                return exportDetails(arr)
            }).then(response => {
              this.loadingText = ""
                  this.loading = false;
                this.download(response.data);
            }).catch(function () { });
            // }
        },

      /** 导出按钮操作 */
      handleExportForBusiness() {
        let arr = {
          startTime: this.queryParams.timeArr ? this.queryParams.timeArr[0] : '',
          endTime: this.queryParams.timeArr ? this.queryParams.timeArr[1] : '',
        }
        this.$confirm('是否确认导出所有数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(()=> {
          this.loadingText = "正在导出中..."
          this.loading = true;
          return exportDetailsForBusiness(arr)
        }).then(response => {
          this.loadingText = ""
                  this.loading = false;
          this.download(response.data);
        }).catch(function () { });
        // }
      },
        /** 获取公司信息 */
        getCompanyList() {
            getCompanys().then(response => {
                this.companys = response.data;
            });
        },
    }
};
</script>
